package com.situ.service;

import com.situ.dao.MenuDao;
import com.situ.entity.User;
import com.situ.vo.MenuTreeVO;
import com.situ.vo.MenuVO;
import com.situ.vo.TableVO;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class MenuService {

    MenuDao dao = new MenuDao();

    public TableVO select() {
        TableVO vo = new TableVO();
        List<MenuVO> list = dao.select();
        List<MenuVO> list2 = new ArrayList<>();
        for (MenuVO m : list) {
            if (m.getParent_id() == 0) {
                m.setChildren(new ArrayList<>());
                list2.add(m);
            }
        }
        for (MenuVO m2 : list2) {
            for (MenuVO m : list) {
                if (m2.getId() == m.getParent_id()) {
                    m2.getChildren().add(m);
                }
            }
        }
        vo.setData(list2);
        return vo;
    }

    public List<MenuTreeVO> tree(Integer rid) {
        List<MenuTreeVO> list = new ArrayList<>();
        //1.先查一级
        list = dao.selectMenu(rid, 0);
        for (MenuTreeVO m : list) {
            m.setChecked(false);
            m.setChildren(dao.selectMenu(rid, m.getId()));
        }
        return list;
    }

    public List<MenuTreeVO> init(HttpSession session) {
        Object o = session.getAttribute("current");
        List<MenuTreeVO> list = dao.selectMenu2(((User) o).getId(), 0);
        for (MenuTreeVO m : list) {
            m.setChildren(dao.selectMenu2(((User) o).getId(), m.getId()));
        }
        return list;
    }
}
